﻿using System;
using System.Collections.Generic;
using System.Text;
using ReliableSession;
using System.Threading;
using log4net;
using System.IO;
using ReliableSession.src;

namespace RollbackConsole
{
    /// <summary>
    /// Test the ReliableSession protocol
    /// </summary>
    public class TestSession
    {
        private Transport server;
        private static readonly ILog log = LogManager.GetLogger(typeof(TestSession));

        public TestSession( )
        {
            try
            {
                Thread.CurrentThread.Name = "Client";
                server = new Transport();
                server.m_debug = true;
                int serverPort = 52524;
                server.openServerSocket(serverPort);

                Thread MyNewThread = new Thread(new ThreadStart(acceptThread));
                MyNewThread.Start();
                runClient(serverPort);
            }
            catch (Exception e)
            {
                log.Error(e);
            }
        }

        private static void runClient(int serverPort)
        {
            Transport client = new Transport();
            //client.openClientSocket("127.0.0.1", serverPort);
            client.openClientSocket("192.168.0.192", serverPort);
            byte[] d = new byte[64 * 1024];
            int chunk = 0;
            int totalLen = 0;
            using (BinaryWriter file = new BinaryWriter(File.Open("out.dat",FileMode.Create)))
            {
                while (true)
                {
                    int len = client.read(d, 0, d.Length);
                    if (len > 0)
                    {
                        //String str = Encoding.ASCII.GetString(d, 0, len);
                        file.Write(d,0,len);
                        file.Flush();
                        chunk += len;
                        if (chunk > 64 * 1024)
                        {
                            totalLen += chunk;
                            chunk = 0;
                            log.Info("Got data = " + totalLen);
                        }
                        //log.Info(str);
                    }
                }
            }
        }

        public void acceptThread()
        {
            try
            {
                Thread.CurrentThread.Name = "Server";
                Transport transport = server.accept();
                transport.m_debug = true;
                byte[] data = new byte[64 * 1024];
                //for(int i=1000; i< 2500; i++)
                using(BinaryReader reader = new BinaryReader(File.Open("../../testData/test1.data",FileMode.Open)))
                {
                    int len = 0;
                    while ((len = reader.Read(data, 0, 450)) > 0)
                    {
                        transport.send(data, 0, len);
                    }
                }
                Thread.Sleep(500);
                transport.close();
                TransportStatistics stats = transport.m_statistics;
                log.Info(stats.ToString() );
            }
            catch (Exception e)
            {
                log.Error(e);
            }
        }
    }
}
